﻿using System;
using System.Web;
using System.Web.DynamicData;
using System.Web.UI.WebControls;
using PD.DataAccess;

namespace PD.Web.DynamicData.PageTemplates
{
    /// <summary>
    /// Page that displays entity details.
    /// </summary>
    public partial class Details : System.Web.UI.Page
    {
        /// <summary>
        /// The metadata that describes a table for use by Dynamic Data pages.
        /// </summary>
        private MetaTable table;

        /// <summary>
        /// Gets the table.
        /// </summary>
        public MetaTable Table
        {
            get { return this.table; }
        }

        /// <summary>
        /// Handles the Init event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Init(object sender, EventArgs e)
        {
            this.table = DynamicDataRouteHandler.GetRequestMetaTable(this.Context);
            this.FormView1.SetMetaTable(this.table);
            this.DetailsDataSource.EntityTypeName = this.table.EntityType.AssemblyQualifiedName;
            this.FormView1.DataBound += new EventHandler(this.FormView1_DataBound);
        }

        /// <summary>
        /// Handles the DataBound event of the FormView1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void FormView1_DataBound(object sender, EventArgs e)
        {
            DynamicHyperLink editLink = this.FormView1.FindControl("EditLink") as DynamicHyperLink;
            if (this.table.EntityType.FullName == typeof(Task).FullName)
            {
                Task task = this.FormView1.DataItem as Task;
                if (editLink != null && task != null)
                {
                    editLink.Visible =
                        task.Status != TaskStatus.Approved &&
                        task.Status != TaskStatus.Rejected &&
                        task.AssignedTo == HttpContext.Current.ProviderUserKey().Value;
                }
            }
            else if (this.table.EntityType.FullName == typeof(Invoice).FullName)
            {
                LinkButton deleteLink = this.FormView1.FindControl("DeleteLink") as LinkButton;
                Invoice invoice = this.FormView1.DataItem as Invoice;
                editLink.Visible = deleteLink.Visible =
                    invoice.Status == InvoiceStatus.Incorrect ||
                    invoice.Status == InvoiceStatus.WaitingForCorrectInfo;
            }
        }

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = this.table.DisplayName;
        }

        /// <summary>
        /// Handles the ItemDeleted event of the FormView1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.FormViewDeletedEventArgs"/> instance containing the event data.</param>
        protected void FormView1_ItemDeleted(object sender, FormViewDeletedEventArgs e)
        {
            if (e.Exception == null || e.ExceptionHandled)
            {
                this.Response.Redirect(this.table.ListActionPath);
            }
        }
    }
}
